import 'dart:async';

import 'package:flutter/material.dart';
import 'package:wechat_kit/wechat_kit.dart';

const String WECHAT_APPID = 'wx854345270316ce6e';
const String WECHAT_UNIVERSAL_LINK = "wechatwx854345270316ce6e"; // iOS 请配置
const String WECHAT_APPSECRET = 'ce4c6c4f007cf10baad3f600da16aa8e';
const String WECHAT_MINIAPPID = 'gh_wxd930ea5d5a258f4f';

class PayScreen extends StatefulWidget {
  @override
  _PayScreenState createState() => _PayScreenState();
}

class _PayScreenState extends State<PayScreen> {

  Wechat _wechat = Wechat()
  ..registerApp(appId: WECHAT_APPID, universalLink: WECHAT_UNIVERSAL_LINK);

  StreamSubscription<WechatPayResp> _pay;

  @override
  void initState() {
    super.initState();
    _pay = _wechat.payResp().listen(_listenPay);
  }

  void _listenPay(WechatPayResp resp) {
    String content = 'pay: ${resp.errorCode} ${resp.errorMsg}';
    _showTips('支付', content);
  }

  void _showTips(String title, String content) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text(title),
          content: Text(content),
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("支付测试"),
      ),
      body: Column(
        children: [
          FlatButton(
            child: Text("微信支付"),
            onPressed: (){
              _wechat.pay(
                appId: WECHAT_APPID,
                partnerId: '商户号',
                prepayId: '预支付交易会话ID',
                package: 'Sign=WXPay',
                nonceStr: '随机字符串, 随机字符串，不长于32位',
                timeStamp: '时间戳：东八区，单位秒',
                sign: '签名',
              );
            },
          ),
          FlatButton(
            child: Text("支付宝"),
            onPressed: (){
              
            },
          ),
        ],
      ),
    );
  }
}